$(function(){
    //获取用户
    getUser();
    //获取购物车信息
    getCarGood();

});
//获取购物车信息
function getCarGood(){
    var userMess=JSON.stringify(ym.user);
    $(".carGoodsDiv").html("");
    ym.ajax({
        url:'/shop/shopCar/getAll',
        param:{
            ymUser:userMess,
        },
        success:function(data){
          console.log(data);
          console.log("获取购物车信息");
          if(data.obj!=null&&data.obj.length>0){
              $(".noGoods").addClass("hide");
              $(".hasGodds").removeClass("hide");
              var html="";
              for(var i=0;i<data.obj.length;i++){
                  var l=data.obj[i];
                  html+='<li class="goodsLi" style="height:110px;padding:15px 10px;">'
                  html+='<span class="goodsImgDiv">'
                  html+='<img src="'+l.productPic+'" class="goodsImg" />'
                  html+='</span>'
                  html+='<span class="goodsMessDivs">'
                  html+='<span class="goodName" style="height:40px;">'
                  html+='<span class="goodsName ar-left" style="width:85%">'+l.productName+'</span>'
                  html+='<span class="delOrder ar-right" style="width:15%;text-align:right" onclick="delGoods('+l.shopCarId+')">'
                  html+='<img src="../../img/shop/delIcon.png" style="height:20px;" />'
                  html+='</span>'
                  html+='</span>'
                  html+='<span class="goodsNumDiv" style="height:40px;">'
                  var price=(l.productPrice*0.01).toFixed(2);
                  html+='<span class="goodMoneyDiv ar-left" style="margin-top:5px;">￥<span class="unitPrice">'+price+'</span>（<span class="intearge">'+l.productPoint+'</span>积分）</span>'
                  html+='<span class="numSpan ar-right" style="margin:0;" shopCarId="'+l.shopCarId+'">'
                  if(l.shopCarProductNumber==1){
                      html+='<span class="reduceNum bagReduce">-</span>'
                  }else{
                      html+='<span class="reduceNum">-</span>'
                  }
                  html+='<input type="number" class="numInput" value="'+l.shopCarProductNumber+'" min="1"  />'
                  html+='<span class="addNum">+</span>'
                  html+='</span></span></span></li>'
              }
              $(".carGoodsDiv").append(html);
              sumMoney();
          }else{
              $(".hasGodds").addClass("hide");
              $(".noGoods").removeClass("hide");
          }
          /*减数量*/
          $(".reduceNum").click(function(){
                var num=$(this).parent(".numSpan").find(".numInput").val();
                if(num==2){
                    $(this).addClass("bagReduce")
                }
                if(num==1){
                    return false;
                }else{
                    num--;
                    $(this).parent(".numSpan").find(".numInput").val(num);
                    $(this).parents(".goodsLi").find(".goodsNum").html("X "+num);
                    var shopCarId=$(this).parent(".numSpan").attr("shopCarId");
                    ym.ajax({
                        url:'/shop/shopCar/update',
                        param:{
                            shopCarId:shopCarId,
                            number:num,
                        },
                        success:function(data){
                            console.log(data);
                        }
                    });
                }
                sumMoney();
          })
          /*加数量*/
          $(".addNum").click(function(){
                $(this).parent(".numSpan").find(".reduceNum").removeClass("bagReduce");
                var num=$(this).parent(".numSpan").find(".numInput").val();
                num++;
                $(this).parent(".numSpan").find(".numInput").val(num);
                $(this).parents(".goodsLi").find(".goodsNum").html("X "+num);
                var shopCarId=$(this).parent(".numSpan").attr("shopCarId");
                ym.ajax({
                      url:'/shop/shopCar/update',
                      param:{
                          shopCarId:shopCarId,
                          number:num,
                      },
                      success:function(data){
                          console.log(data);
                      }
                });
                sumMoney();
          })
        //数量输入的失去焦点事件
        $(".numInput").blur(function(){
            var num=$(this).val();
            if(num<1||!/^\d+$/.test(num)){
                $(this).val("1");
                num=1;
            }
            var shopCarId=$(this).parent(".numSpan").attr("shopCarId");
            ym.ajax({
                url:'/shop/shopCar/update',
                param:{
                    shopCarId:shopCarId,
                    number:num,
                },
                success:function(data){
                    console.log(data);
                }
            });
            sumMoney();

        })
        }
    });
}
//计算总价
function sumMoney(){
    var allMoney=0;
    var allIntearge=0;
    $(".goodsLi").each(function(){
        var SingleGoodMoney=$(this).find(".unitPrice").html();
        var SingleGoodIntearge=$(this).find(".intearge").html();
        SingleGoodMoney=parseFloat(SingleGoodMoney);
        SingleGoodIntearge=parseInt(SingleGoodIntearge);
        var SingleGoodNum=$(this).find(".numInput").val();
        SingleGoodNum=parseInt(SingleGoodNum);
        allMoney+=(SingleGoodMoney*SingleGoodNum);
        allIntearge+=(SingleGoodIntearge*SingleGoodNum);
    })
    allMoney=allMoney.toFixed(2);
    $(".endMoney .money").html(allMoney);
    $(".endMoney .allIntearge").html(allIntearge);
}
//删除
function delGoods(shopCarId){
    $('#myConfirm .am-modal-bd').html("您是否确定删除此商品？")
    var $confirm = $('#myConfirm');
    var confirm = $confirm.data('amui.modal');
    var onConfirm = function() {
        //点击确认按钮调用事件
        ym.ajax({
            url:'/shop/shopCar/del',
            param:{
                shopCarId:shopCarId,
            },
            success:function(data){
                console.log(data)
                if(data.res==1){
                    $("#alertModal .am-modal-bd").html("删除成功！");
                    $("#alertModal").modal();
                    getCarGood();
                    setTime();
                }
            }
        });

    };
    var onCancel = function() {
        //取消按钮调用事件
    };
    if (confirm) {
        confirm.options.onConfirm =  onConfirm;
        confirm.options.onCancel =  onCancel;
        confirm.toggle(this);
    } else {//重定义模态窗函数
        $confirm.modal({
            relatedElement: this,
            closeViaDimmer: false,//禁止点击遮罩层关闭
            onConfirm: onConfirm,
            onCancel: onCancel
        });
    }
}
/*两秒后关闭弹窗*/
function setTime(){
    setTimeout(function () {
        $("#alertModal").modal('close');
    }, 2000);
}
//结算
function goSureOrder(){
    window.location.href="sureOrder.html";
}
function pageJump(num){
    if(num==1){
        window.location.href="index.html";
    }else if(num==3){
        window.location.href="person.html";
    }
}

